package io;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

/**
 * @author Cheng Jun
 * Description: ObjectInputStream 反序列化对象
 * @version 1.0
 * @date 2022/8/14 15:50
 * @see
 */
public class ObjectInputStreamExample {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        read();
    }

    private static void read() throws IOException, ClassNotFoundException {
        // dat 文件，表示数据文件。各个语言对dat数据的处理不一样。
        FileInputStream fileInputStream = new FileInputStream("src/io/TestFile/ForObjectInputStream.dat");
        ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
        // 反序列化的顺序要和 序列化时的顺序一致。
        Object student = objectInputStream.readObject();
        if (student instanceof Student) {
            System.out.println((Student) student);
        } else {
            System.out.println("类型转换错误");
        }
    }
}
